package com.huatec.service.impl;

import com.huatec.dao.StudentDao;
import com.huatec.entity.Student;
import com.huatec.service.StudentService;
import com.huatec.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    //注入Student Dao
    @Resource
    private StudentDao studentDao;

    //加载所有学生列表
    public Result loadStudentList(int offset, int limit) {
        // TODO Auto-generated method stub
        Result result = new Result();

        List<Student> students = studentDao.queryAll(offset, limit);
        for (Student s : students) {
            System.out.println(s);
        }

        result.setStatus(0);
        result.setMsg("加载全校学生列表信息成功");
        result.setData(students);
        return result;
    }

    //加载班级学生列表
    public Result loadStudentList(int classId) {
        // TODO Auto-generated method stub
        // TODO Auto-generated method stub
        Result result = new Result();
        List<Student> students = studentDao.findByClassId(classId);
        for (Student s : students) {
            System.out.println(s);
        }
        //Stu Class stu Class=stu Class Dao.find All();
        result.setStatus(0);
        result.setMsg("加载该班级学生列表信息成功");
        result.setData(students);
        return result;
    }

    //关键字搜索学生信息
    public Result loadStudentList(String keywords) {
        // TODO Auto-generated method stub
        Result result = new Result();
        String keywords1 = "%" + keywords + "%";
        List<Student> students = studentDao.queryByWhereSql(keywords1);
        /*　　for(Student s:students){
63 System.out.println(s);
64 }*/
         result.setStatus(0);
         result.setMsg("通过关键字搜索学生列表成功");
         result.setData(students);
        return result;
         }
 //增加学生信息
        public Result addStudent(String studentNum, String studentName,String username, String password,
int studentSex, String mobilePhone, String email, int classId) {
        // TODO Auto-generated method stub
        Result result=new Result();
            Student findstu=studentDao.findByNum(studentNum);
            if(findstu!=null){
                result.setStatus(1);
                result.setMsg("添加学生信息失败，此学号学生已存在");
                return result;
            }
        Student student= new Student();
         student.setStudentNum(studentNum);
         student.setStudentName(studentName);
         student.setUsername(username);
         student.setPassword(password);
         student.setStudentSex(studentSex);
         student.setMobilePhone(mobilePhone);
         student.setEmail(email);
         student.setClassId(classId);

         studentDao.addStudent(student);
         result.setStatus(0);
         result.setMsg("新增学生成功");
         result.setData(student);
       return result;
         }
 //删除学生信息
        public Result deleteStudentById(int studentId) {
        // TODO Auto-generated method stub
         Result result=new Result();

         Student student=studentDao.queryById(studentId);
         if(student==null){
             result.setStatus(1);
             result.setMsg("此学生不存在");
             return result;
             }
         studentDao.deleteStudent(studentId);

         result.setStatus(0);
         result.setMsg("删除学生成功");
         return result;
         }
 //修改学生信息
         public Result updateStudent(int studentId, String studentNum,String studentName,
                                     String username, String password,String mobilePhone, String email) {
        // TODO Auto-generated method stub
        Result result=new Result();

         Student student=studentDao.queryById(studentId);
         if(student==null){
             result.setStatus(1);
             result.setMsg("此学生不存在");
             return result;
         }

         student.setStudentId(studentId);
         String studentNum1=(studentNum==null)?student.getStudentNum():studentNum;
         student.setStudentNum(studentNum1);
         String studentName1=(studentName==null)?student.getStudentName():studentName;
         student.setStudentName(studentName1);
         String username1=(username==null)?student.getUsername():username;
         student.setUsername(username1);
         String password1=(password==null)?student.getPassword():password;
         student.setPassword(password1);
//         student.setStudentSex(studentSex);
         String mobilePhone1=(mobilePhone==null)?student.getMobilePhone():mobilePhone;
         student.setMobilePhone(mobilePhone1);
         String email1=(email==null)?student.getEmail():email;
         student.setEmail(email1);
         studentDao.modifyStudent(student);
         result.setStatus(0);
         result.setMsg("修改学生成功");
        result.setData(student);

         return result;
    }
//通过student Id查看学生
         public Result loadStudentById(int studentId) {
         // TODO Auto-generated method stub
         Result result=new Result();

         Student student=studentDao.queryById(studentId);
         if(student==null){
             result.setStatus(1);
             result.setMsg("此学生不存在");
             return result;
             }

         result.setStatus(0);
         result.setMsg("查询学生信息成功");
         result.setData(student);
         return result;
         }

         //新增一个功能：通过student Num查询学生，并将数据提供给饭卡管理系统用
         public Result loadStudentByNum(String studentNum) {
         // TODO Auto-generated method stub
         Result result=new Result();

         Student student=studentDao.findByNum(studentNum);
         if(student==null){
             result.setStatus(1);
             result.setMsg("此学生不存在");
            return result;
             }

        result.setStatus(0);
        result.setMsg("查询学生成功");
        result.setData(student);
         return result;
         }
 }

